package cn.doitedu.day07

import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.expressions.Window
import org.apache.spark.sql.functions._

object T26_DSLContinuedLogin {

  def main(args: Array[String]): Unit = {
    val days = 3
    val spark = SparkSession.builder().appName(this.getClass.getSimpleName)
      .master("local[4]")
      .getOrCreate()

    //创建DataFrame
    val df = spark.read
      .option("header", "true")
      .csv("data/login2.txt")

    import spark.implicits._

    df.distinct()
      .select(
        $"uid",
        $"dt",
        row_number() over(Window.partitionBy($"uid").orderBy($"dt")) as "rn"
      )
      .select(
        'uid,
        'dt,
        date_sub('dt, 'rn) as "date_dif"
      )
      .groupBy("uid", "date_dif")
      .agg(
        count("*") as "counts",
        min("dt") as "start_dt",
        max("dt") as "end_dt",
      )
      .where($"counts" >= 3)
      .drop("date_dif")
      .show()




  }

}
